package alipayex

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/go-pay/gopay"
)

// IotIdentificationUserQuery zoloz.identification.user.web.query(刷脸认证查询)
// 文档地址：https://opendocs.alipay.com/apis/009yws
func (a *Client) IotIdentificationUserQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *IotIdentificationUserQueryResponse, err error) {
	err = bm.CheckEmptyError("biz_id", "zim_id")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.doAliPay(ctx, bm, "zoloz.identification.user.web.query"); err != nil {
		return nil, err
	}
	fmt.Println("IotIdentificationUserQuery", string(bs))
	aliRsp = new(IotIdentificationUserQueryResponse)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	return aliRsp, nil
}

// IotVspUserCreate iotvsp.user.create(录入机构用户信息)
// 文档地址：https://opendocs.alipay.com/pre-apis/034wk5
func (a *Client) IotVspUserCreate(ctx context.Context, bm gopay.BodyMap) (aliRsp *IotVspUserCreateResponse, err error) {
	err = bm.CheckEmptyError("isv_pid", "org_out_id", "component_out_id")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.doAliPay(ctx, bm, "alipay.open.iotvsp.user.create"); err != nil {
		return nil, err
	}
	aliRsp = new(IotVspUserCreateResponse)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	return aliRsp, nil
}

// IotVspUserDelete alipay.open.iotvsp.user.delete(机构用户出库)
// 文档地址：https://opendocs.alipay.com/pre-apis/034a9i
func (a *Client) IotVspUserDelete(ctx context.Context, bm gopay.BodyMap) (aliRsp *IotVspUserDeleteResponse, err error) {
	err = bm.CheckEmptyError("isv_pid", "org_out_id")
	if err != nil {
		return nil, err
	}
	var bs []byte
	if bs, err = a.doAliPay(ctx, bm, "alipay.open.iotvsp.user.delete"); err != nil {
		return nil, err
	}
	aliRsp = new(IotVspUserDeleteResponse)
	if err = json.Unmarshal(bs, aliRsp); err != nil {
		return nil, err
	}
	if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
		info := aliRsp.Response
		return aliRsp, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
	}
	return aliRsp, nil
}
